<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>导入分部清单</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>分部名称</label>

                        <div class="layui-input-block">
                            <input type="text" name="qingname" id="qingname" class="layui-input">
                            <div class="choose_div" id="budid" key="1"></div>
                            <span class="choose_one" onclick="shortTabExcel()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>清单信息</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>分部行</th>
                            <th>子分部行</th>
                            <th>项目编码</th>
                            <th>项目名称</th>
                            <th>项目特征描述</th>
                            <th>计量单位</th>
                            <th>工程量</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_enginid = getQueryVariable('enginid') || 0;




    $("#commentForm").validate({
        rules: {
            qingname: {required: true},
        },
        messages:{
            qingname: {required: "请输入"},
        }
    });



    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var fenname = $('#list_list .fenname').eq(i).val();
                    var ziname = $('#list_list .ziname').eq(i).val();
                    var encode = $('#list_list .encode').eq(i).val();
                    var enname = $('#list_list .enname').eq(i).val();
                    var enfeat = $('#list_list .enfeat').eq(i).val();
                    var enunit = $('#list_list .enunit').eq(i).val();
                    var enword = $('#list_list .enword').eq(i).val();

                    list.push({
                        fenname:fenname,
                        ziname:ziname,
                        encode:encode,
                        enname:enname,
                        enfeat:enfeat,
                        enunit:enunit,
                        enword:enword
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.enginid = param_enginid;

            facade.ajax({url: ask_main_qing_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    facade.success("提交成功!");
                    setTimeout(() => {
                        try {
                            parent.layui.table.reload('datalist_nav');
                            parent.layui.table.reload('datalist');
                        }catch (e) {

                        }
                        layer_close_opt();
                    }, 1500);
                }
            });

            return false;
        });

    });



    function short_table_excel_list(qingname, return_data, fieldArr){
        $("#qingname").val(qingname);
        var trStr = '';
        var key = 0;
        for (var i=0; i<return_data.length; i++){
            for (var j=0; j<fieldArr.length; j++){
                if (typeof return_data[i][fieldArr[j].field] == "undefined") {
                    return_data[i][fieldArr[j].field] = '';
                }
            }
            if(return_data[i].datatype == '清单行'){
                key = key + 1;
                trStr += '<tr>';
                trStr += '<td style="width: 45px">'+key+'</td>';
                trStr += '<td><input readonly type="text" class="layui-input fenname" value="'+return_data[i].fenname+'"></td>';
                trStr += '<td><input readonly type="text" class="layui-input ziname" value="'+return_data[i].ziname+'"></td>';
                trStr += '<td><input readonly type="text" class="layui-input encode" value="'+return_data[i].encode+'"></td>';
                trStr += '<td><input readonly type="text" class="layui-input enname" value="'+return_data[i].enname+'"></td>';
                trStr += '<td><input readonly type="text" class="layui-input enfeat" value="'+return_data[i].enfeat+'"></td>';
                trStr += '<td><input readonly type="text" class="layui-input enunit" value="'+return_data[i].enunit+'"></td>';
                trStr += '<td><input readonly type="text" class="layui-input enword" value="'+return_data[i].enword+'"></td>';
                trStr += '</tr>';
            }
        }
        $("#list_list").html(trStr);
    }
    var dialogkey = 0;
    var short_table_excel_header_obj = [
        { field: "encode", name: "项目编码", ismust: 1,type: ""},
        { field: "enname", name: "项目名称", ismust: 1,type: ""},
        { field: "enfeat", name: "项目特征描述", ismust: 0,type: ""},
        { field: "enunit", name: "计量单位", ismust: 1,type: ""},
        { field: "enword", name: "工程量", ismust: 1,type: ""},
    ];

    function shortTabExcel() {
        open_page('qing_import.php?header_obj=short_table_excel_header_obj&call_func=short_table_excel_list', '导入文件', '95%', '95%');
    }

    function table_opt_func(table_key){
        if(!table_key || table_key == undefined || table_key == 'undefined'){
            table_key = 1;
        }
        update_refresh_row_func();
        $(".short_table_del_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                layer.msg("请选中要删除的行");return false;
            }
            layer.confirm("确定要删除此行信息?",function (conindex) {
                $(sle_obj).remove();
                if($(this).parents('.short_table_list').find('table').children('tbody').children('tr').length < 1){
                    $(this).parents('.short_table_list').children('table').children('tbody').append('');
                }

                $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                    $(this).children('td:first-child').html(key+1);
                });
                render_date();
                render_from();
                update_refresh_row_func();
                layer.close(conindex);
            });
        });
        $(".short_table_del_all").click(function () {
            var that = $(this);
            if(that.parents('.short_table_list').attr('key') != table_key){
                return false;
            }

            layer.confirm("确定要删除全部信息?",function (conindex) {
                var sle = false;
                var sle_obj;
                that.parents('.short_table_list').find('table').children('tbody').html('');

                that.parents('.short_table_list').find('table').children('tbody').append('');
                $.each(that.parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                    $(this).children('td:first-child').html(key+1);
                });
                render_date();
                render_from();
                update_refresh_row_func();
                layer.close(conindex);
            });
        });
    }

</script>
</body>
</html>
